home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / sswitchxp152.exe / source / SpeedswitchXP / SpeedswitchXP.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-14  |  6.3 KB  |  246 lines

  1. /*
  2.    SpeedswitchXP V1.5
  3.    - Windows XP CPU Frequency Control for Notebooks -
  4.  
  5.    Copyright(c) 2002-2005 Christian Diefer
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License version 2 as 
  9.    published by the Free Software Foundation.
  10.    
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.    
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #define _UNICODE
  22.  
  23. #include "stdafx.h"
  24. #include "SpeedswitchXP.h"
  25. #include "TOptions.h"
  26. #include "SpeedswitchXPDlg.h"
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #endif
  31.  
  32. TCHAR logPath[264];
  33.  
  34. // CSpeedswitchXPApp
  35.  
  36. BEGIN_MESSAGE_MAP(CSpeedswitchXPApp, CWinApp)
  37.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  38. END_MESSAGE_MAP()
  39.  
  40.  
  41. //---------------------------------------------------------------------------
  42. // CSpeedswitchXPApp-Erstellung
  43. //---------------------------------------------------------------------------
  44. CSpeedswitchXPApp::CSpeedswitchXPApp()
  45. {
  46.   activeMutex = FALSE;
  47. }
  48.  
  49. // Das einzige CSSpeedswitchXPApp-Objekt
  50. CSpeedswitchXPApp theApp;
  51. CSpeedswitchXPDlg* dlg;
  52. static CString error;
  53.  
  54. //---------------------------------------------------------------------------
  55. // CSpeedswitchXPApp Initialisierung
  56. //---------------------------------------------------------------------------
  57. BOOL CSpeedswitchXPApp::InitInstance()
  58. {
  59.     // InitCommonControls() ist fⁿr Windows XP erforderlich, wenn ein Anwendungsmanifest
  60.     // die Verwendung von ComCtl32.dll Version 6 oder h÷her zum Aktivieren
  61.     // von visuellen Stilen angibt. Ansonsten treten beim Erstellen von Fenstern Fehler auf.
  62.     InitCommonControls();
  63.  
  64. //  m_hInstResDLL = LoadLibraryW( _T("SpeedswitchXPDEU.dll") );
  65. //  ASSERT( m_hInstResDLL != NULL );
  66.  
  67. //  AfxSetResourceHandle( m_hInstResDLL );
  68.  
  69.     CWinApp::InitInstance();
  70.  
  71.     // make sure that only one instance is running
  72.   BOOL    ret = FALSE;
  73.     TCHAR* mutexName = _T("SpeedswitchXP_mutex");
  74.  
  75.   hMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, mutexName );
  76.     if( hMutex == NULL )
  77.     {
  78.         hMutex = CreateMutex( 0, TRUE, mutexName );
  79.         DWORD err = GetLastError();
  80.         ret = !(hMutex==NULL || err==ERROR_ALREADY_EXISTS);
  81.     if( hMutex != NULL )
  82.       activeMutex = TRUE;
  83.     }
  84.     else
  85.         CloseHandle( hMutex );
  86.  
  87.     if( ret == FALSE )
  88.   {
  89.     CString s1, s2;
  90.     s1.LoadStringW( IDS_START1 );
  91.     s2.LoadStringW( IDS_START2 );
  92.     MessageBox( NULL, s1, s2, MB_ICONSTOP|MB_OK );
  93.     return FALSE;
  94.   }
  95.  
  96.   error.LoadStringW( IDS_MAINERR2 );
  97.   
  98.   // create log filename
  99.   if( GetModuleFileName(NULL,logPath,255) == 0 )
  100.   {
  101.     CString s1;
  102.     s1.LoadStringW( IDS_START3 );
  103.     CloseHandle( hMutex );
  104.     MessageBox( NULL, s1, error, MB_OK|MB_ICONSTOP );
  105.     return FALSE;
  106.   }
  107.  
  108.   int i = (int)_tcslen(logPath) - 1;
  109.   while( i>=0 && logPath[i]!='.' )
  110.     i--;
  111.  
  112.   if( i >= 0 )
  113.     _tcscpy_s( &logPath[i], 264-i-3, _T(".log") );
  114.   else
  115.   {
  116.     CString s1;
  117.     s1.LoadStringW( IDS_START4 );
  118.     CloseHandle( hMutex );
  119.     MessageBox( NULL, s1, error, MB_ICONSTOP|MB_OK );
  120.     return FALSE;
  121.   }
  122.  
  123.   // create and display the main window
  124.   dlg = new CSpeedswitchXPDlg();
  125.   m_pMainWnd = dlg;
  126.  
  127.   if( m_pMainWnd )
  128.   {
  129.     log( _T("Mutex: 0x%08x"), hMutex );
  130.     log( _T("Init complete") );
  131.     log( _T("--------------------------------") );
  132.     dlg->setCmdLine( m_lpCmdLine );
  133.     return TRUE;
  134.   }
  135.   else
  136.     return FALSE;
  137. }
  138.  
  139. //---------------------------------------------------------------------------
  140. // Terminate program instance
  141. //---------------------------------------------------------------------------
  142. int CSpeedswitchXPApp::ExitInstance() 
  143. {
  144.   if( activeMutex )  
  145.   {
  146.     log( _T("/bReleasing mutex (0x%08x)... "), hMutex );
  147.     if( CloseHandle(hMutex) )
  148.       log( _T("/aOk") );
  149.     else
  150.       log( _T("/aError") );
  151.   }
  152.  
  153.   //FreeLibrary( m_hInstResDLL );
  154.  
  155.     return CWinApp::ExitInstance();
  156. }
  157.  
  158. //---------------------------------------------------------------------------
  159. // print out a debug message
  160. //---------------------------------------------------------------------------
  161. void log( TCHAR* msg, ... )
  162. {
  163.   if( !options.debugMode )
  164.     return;
  165.  
  166.   BOOL noDate=FALSE, noCR=FALSE;
  167.   static TCHAR debugBuf[32768];
  168.   va_list ap;
  169.   va_start( ap, msg );
  170.  
  171.   time_t zeit = time( NULL );
  172.   struct tm ts;
  173.   int err = localtime_s( &ts, &zeit );
  174.  
  175.   *debugBuf = '\0';
  176.  
  177.   if( *msg == '/' )
  178.   {
  179.     switch( msg[1] )
  180.     {
  181.       case 'a': noDate=TRUE;
  182.                 msg+=2;  
  183.                 break;
  184.  
  185.       case 'b': noCR=TRUE;
  186.                 msg+=2;  
  187.                 break;
  188.  
  189.       case 'c': noDate=TRUE;
  190.                 noCR=TRUE;
  191.                 msg+=2;  
  192.                 break;
  193.     }
  194.   }
  195.  
  196.   if( !noDate )
  197.     wsprintf( debugBuf,
  198.               _T("%02d:%02d:%02d "),
  199.               ts.tm_hour,
  200.               ts.tm_min,
  201.               ts.tm_sec );
  202.  
  203.   _vstprintf_s( &debugBuf[_tcslen(debugBuf)], 32768-_tcslen(debugBuf)-1, msg, ap );
  204.   va_end( ap );
  205.  
  206.   if( !noCR )
  207.     _tcscat_s( debugBuf, _T("\r\n") );
  208.  
  209.   HANDLE hdl = CreateFile( logPath, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  210.   if( hdl == INVALID_HANDLE_VALUE )
  211.   {
  212.     options.debugMode = FALSE;
  213.     CString s1;
  214.     s1.LoadStringW( IDS_START5 );
  215.     wsprintf( debugBuf, s1, GetLastError() );
  216.     MessageBox( NULL, debugBuf, error, MB_ICONSTOP|MB_OK );
  217.     return;
  218.   }
  219.  
  220.   if( SetFilePointer(hdl,0,NULL,FILE_END) == INVALID_SET_FILE_POINTER )
  221.   {
  222.     CloseHandle( hdl );
  223.     options.debugMode = FALSE;
  224.     CString s1;
  225.     s1.LoadStringW( IDS_START6 );
  226.     wsprintf( debugBuf, s1, GetLastError() );
  227.     MessageBox( NULL, debugBuf, error, MB_ICONSTOP|MB_OK );
  228.     return;
  229.   }
  230.  
  231.   DWORD written;
  232.   if( WriteFile(hdl,debugBuf,(DWORD)_tcslen(debugBuf),&written,NULL) == 0 )
  233.   {
  234.     CloseHandle( hdl );
  235.     options.debugMode = FALSE;
  236.     CString s1;
  237.     s1.LoadStringW( IDS_START7 );
  238.     wsprintf( debugBuf, s1, GetLastError() );
  239.     MessageBox( NULL, debugBuf, error, MB_ICONSTOP|MB_OK );
  240.     return;
  241.   }
  242.  
  243.   CloseHandle( hdl );
  244. }
  245.  
  246.